﻿
using System;
using UnityEngine;
using Yoozoo.Gta.Common;

public class NotchUISelfFix : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        checkAndFix();
    }

    private void checkAndFix()
    {
        double currAspect = Math.Round((float) Screen.width / Screen.height, 2);
        double realAspect = Math.Round(DeviceInfo.GetRealScreenAspect(), 2);
        if (!currAspect.Equals(realAspect))
        {
            //当前的屏幕比例和屏幕的物理比例不一致 则表示当前不是全屏状态(可能设置了安全区域不显示)
            //非全屏状态 不检测异形屏
            Debug.LogWarning($"当前应用没有全屏显示 请检查系统设置 curr:{currAspect}  real:{realAspect}");
            return;
        }
        
        GameObject rootCanvasGo = ResidentHandler.Inst.RootCanvas;
        if (rootCanvasGo != null)
        {
            var scaleFactor = ResidentHandler.Inst.RootCanvasComponent.scaleFactor;
            var left = Mathf.Ceil(DeviceInfo.GetNotchHeight() / scaleFactor);
            var rectTrans = (RectTransform)this.transform;
            if (rectTrans.anchorMin == new Vector2(0, 0) && rectTrans.anchorMax == new Vector2(1, 1))
            {
                rectTrans.offsetMin = new Vector2(left, rectTrans.offsetMax.y);
                rectTrans.offsetMax = new Vector2(-left, rectTrans.offsetMax.y);
            }

        }
        
    }

}
